package com.cskaoyan.service;

import com.cskaoyan.bean.user.Address;
import com.cskaoyan.bean.user.AddressExample;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.wxaddress.AddressParam;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.util.DateUtils;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * \* Description:
 * \
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    RegionMapper regionMapper;
    @Autowired
    AddressMapper addressMapper;


    @SuppressWarnings("all")
    @Override
    public BaseData<Address> address(BaseParam param, String name, Integer userId) {
        PageUtil.start(param);

        AddressExample addressExample = new AddressExample();
        addressExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        List<Address> addressList = addressMapper.selectByExample(addressExample);
        PageInfo<Address> pageInfo = new PageInfo<>(addressList);

        BaseData<Address> data = new BaseData<>(pageInfo, addressList);
        return data;

    }

    @Override
    public BaseData<Address> list(Integer userId) {
        List<Address> address = addressMapper.getAddresslist(userId);
        AddressParam param = new AddressParam();
        PageUtil.start(param);
        PageInfo<Address> pageInfo = new PageInfo<>(address);
        BaseData<Address> data = new BaseData<>(pageInfo, address);
        return data;
    }

    @Override
    public Address detail(int userId) {
        Address address = addressMapper.getAddress(userId);
        return address;
    }

    @Override
    public int saveUpdate(Address address) {
        /**
         * 逻辑：
         * 修改name、tel、省市县、地址、是否默认地址 --在表里面会相应的修改地区编码areaCode
         * */
        //修改了省市县的名称---其实是修改了address表里的省市县的id
        //先根据省市县的名称，找到对应的区域ID
        // 在根据区域ID修改为更改后的id
        int saveAddressId = 0;//之后返回给前端的被修改的地址id
        //得到省市县的名
        String province = address.getProvince();
        String city = address.getCity();
        String county = address.getCounty();
        //得到省市县对应的id
        Integer provinceId = regionMapper.getRegionIdByName(province, 0);
        Integer cityId = regionMapper.getRegionIdByName(city, provinceId);
        Integer countyId = regionMapper.getRegionIdByName(county, cityId);
        String areaCode = regionMapper.getAreaCodeByName(county, cityId);
        //剩下要更改的信息
        String addressDetail = address.getAddressDetail();
        Boolean isDefault = address.getIsDefault();
        Integer id = address.getId();
        String name = address.getName();
        String tel = address.getTel();
        Date date = new Date();
        String updateTime = DateUtils.dateFormat(date);
        if (isDefault == null || isDefault == false) {
            isDefault = false;
        } else {
            Integer isDefaultId = addressMapper.selectIdbyDefault();
            int tag = addressMapper.updateIsDefault(isDefaultId);
            if (tag !=0) isDefault = true;
        }
        int num = addressMapper.save(addressDetail, areaCode, provinceId, cityId, countyId, isDefault, name, tel, updateTime, id);
        if (num != 0) {
            saveAddressId = id;
        }
        return saveAddressId;
    }

    @Override
    public int saveAdd(Address address, int userId) {
        int saveAddressId = 0;//之后返回给前端的被修改的地址id
        //得到省市县的名
        String province = address.getProvince();
        String city = address.getCity();
        String county = address.getCounty();
        //得到省市县对应的id
        Integer provinceId = regionMapper.getRegionIdByName(province, 0);
        Integer cityId = regionMapper.getRegionIdByName(city, provinceId);
        Integer countyId = regionMapper.getRegionIdByName(county, cityId);
        String areaCode = regionMapper.getAreaCodeByName(county, cityId);
        //剩下要更改/插入的信息
        String addressDetail = address.getAddressDetail();
        Boolean isDefault = address.getIsDefault();

//        Map map = addressMapper.selectTotal(userId);
        Integer id = address.getId();
        String name = address.getName();
        String tel = address.getTel();
        Date date = new Date();
        String updateTime = DateUtils.dateFormat(date);
        if (isDefault == null || isDefault == false) {
            isDefault = false;
        } else {
            Integer isDefaultId = addressMapper.selectIdbyDefault();
            int tag = addressMapper.updateIsDefault(isDefaultId);
            if (tag !=0) isDefault = true;
        }
        String addTime = DateUtils.dateFormat(new Date());
        int num = addressMapper.saveAdd(addressDetail, userId, areaCode, provinceId, cityId, countyId, isDefault, name, tel, addTime);
        if (num != 0) {
            saveAddressId = id;
        }
        return saveAddressId;
    }

    @Override
    public int delete(Integer id) {
        String updateTime = DateUtils.dateFormat(new Date());
        int num = 0;
        if (id != null) {
            num = addressMapper.delete(id, updateTime);
        }
        return num;
    }
}
